package com.chhuang.system.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chhuang.core.bean.QueryBean;
import com.chhuang.core.service.impl.BaseServiceImpl;
import com.chhuang.system.bean.UserWithRoleIdQueryBean;
import com.chhuang.system.mapper.ChUserRoleMapMapper;
import com.chhuang.system.po.ChUser;
import com.chhuang.system.po.ChUserRoleMap;
import com.chhuang.system.po.ChUserRoleMapExample;
import com.chhuang.system.service.IChUserRoleMapService;

/**
* ChUserRoleMap ServiceImpl
* @author ChHuang
* @date Fri Mar 03 13:20:59 CST 2017
*/
@Service
public class ChUserRoleMapServiceImpl extends BaseServiceImpl<ChUserRoleMapMapper, ChUserRoleMap> implements IChUserRoleMapService {
	private static final Log log = LogFactory.getLog(ChUserRoleMapServiceImpl.class);
	
	@Autowired
	private ChUserRoleMapMapper chUserRoleMapMapper;
	
	@Override
	public QueryBean<ChUserRoleMap> seniorQuery(QueryBean<ChUserRoleMap> queryBean) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public List<ChUserRoleMap> findByUserId(String userId) throws Exception {
		ChUserRoleMapExample example = new ChUserRoleMapExample();
		example.setOrderByClause("role_order asc");
		example.createCriteria().andUserIdEqualTo(userId);
		return chUserRoleMapMapper.selectByExample(example);
	}

	@Override
	public int deleteByUserId(String userId) throws Exception {
		ChUserRoleMapExample example = new ChUserRoleMapExample();
		example.createCriteria().andUserIdEqualTo(userId);
		return chUserRoleMapMapper.deleteByExample(example);
	}

	@Override
	public int deleteByUserIdArray(String[] userIds) throws Exception {
		ChUserRoleMapExample example = new ChUserRoleMapExample();
		example.createCriteria().andUserIdIn(Arrays.asList(userIds));
		return chUserRoleMapMapper.deleteByExample(example);
	}

	@Override
	public List<ChUserRoleMap> findByRoleId(String roleId) throws Exception {
		ChUserRoleMapExample example = new ChUserRoleMapExample();
		example.createCriteria().andRoleIdEqualTo(roleId);
		return chUserRoleMapMapper.selectByExample(example);
	}

	@Override
	public int deleteByRoleIdArray(String[] roleIds) throws Exception {
		ChUserRoleMapExample example = new ChUserRoleMapExample();
		example.createCriteria().andRoleIdIn(Arrays.asList(roleIds));
		return chUserRoleMapMapper.deleteByExample(example);
	}

	@Override
	public int deleteByUserIdRoleId(String userId, String roleId) throws Exception {
		ChUserRoleMapExample example = new ChUserRoleMapExample();
		example.createCriteria().andUserIdEqualTo(userId)
			.andRoleIdEqualTo(roleId);
		return chUserRoleMapMapper.deleteByExample(example);
	}

	@Override
	public QueryBean<ChUser> findUsersByRoleIdAndQueryConditions(UserWithRoleIdQueryBean queryBean) throws Exception {
		queryBean.setItemCount(chUserRoleMapMapper.countUsersByRoleId(queryBean));//总记录数
		if(queryBean.getItemCount()>0){//计算总页数
			queryBean.setPageCount(queryBean.getItemCount()/queryBean.getPageSize()+
					(int)(queryBean.getItemCount()%queryBean.getPageSize()>0?1:0));
			if(queryBean.getPageCount()>=queryBean.getPageNum()){
				//分页
				queryBean.setOffset((queryBean.getPageNum()-1)*queryBean.getPageSize());
				queryBean.setResult(chUserRoleMapMapper.selectUsersByRoleId(queryBean));
			}
		}
		return queryBean;
	}

}